package com.csxy.dyq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.dyq.entity.EduTeacher;
import com.csxy.dyq.entity.vo.TeacherQuery;
import com.csxy.dyq.mapper.EduTeacherMapper;
import com.csxy.dyq.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService{
    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    //分页查询讲师的方法
    @Override
    public Page<EduTeacher> pageListTeacher(long page, long limit) {
        Page<EduTeacher> mypage = new Page<>(page,limit);
        Page<EduTeacher> eduTeacherPage = eduTeacherMapper.selectPage(mypage, null);
        return eduTeacherPage;
    }

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if (teacherQuery == null){
            eduTeacherMapper.selectPage(pageParam,null);
        }
        if (teacherQuery.getName() != null && !"".equals(teacherQuery.getName())){
            queryWrapper.like("name",teacherQuery.getName());
        }
        if (teacherQuery.getLevel() != null && !"".equals(teacherQuery.getLevel())){
            queryWrapper.eq("level",teacherQuery.getLevel());
        }
        if (teacherQuery.getBegin() != null && !"".equals(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if (teacherQuery.getEnd() != null && !"".equals(teacherQuery.getEnd())){
            queryWrapper.le("gmt_modified",teacherQuery.getEnd());
        }
        eduTeacherMapper.selectPage(pageParam,queryWrapper);
    }
    //查询前4条热门讲师
    @Override
    @Cacheable(value = "teacher",key = "'selectHotTeacher'")
    public List<EduTeacher> selectHotTeacher() {
        QueryWrapper<EduTeacher> Wrapper = new QueryWrapper<>();
        Wrapper.orderByDesc("id");
        Wrapper.last("limit 4");
        return baseMapper.selectList(Wrapper);
    }
    //前台查询讲师列表
    @Override
    public Map<String, Object> getTeacherFrontPageList(Page<EduTeacher> teacherPage) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        baseMapper.selectPage(teacherPage, wrapper);
        HashMap<String, Object> map = new HashMap<>();

        long total = teacherPage.getTotal();
        long current = teacherPage.getCurrent();
        long size = teacherPage.getSize();
        List<EduTeacher> teacherList = teacherPage.getRecords();
        long pages = teacherPage.getPages();
        boolean hasPrevious = teacherPage.hasPrevious();
        boolean hasNext = teacherPage.hasNext();
        //封装数据
        map.put("total", total);
        map.put("list", teacherList);
        map.put("size", size);
        map.put("current", current);
        map.put("pages", pages);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);

        return map;
    }
}









